<?php

namespace src\utils;
use Firebase\JWT\JWT;

class JWTUtils
{
    public static function generateToken($user)
    {
        $payload = [
            'iss' => 'blog_system',
            'iat' => time(),
            'exp' => time() + $_ENV['JWT_EXPIRATION'],
            'user' => [
                'id' => $user['id'],
                'username' => $user['username'],
                'role' => $user['role']
            ]
        ];

        return JWT::encode($payload, $_ENV['JWT_SECRET'], 'HS256');
    }


    public static function decodeToken($token)
    {
        // try {
        $decoded = JWT::decode($token, $_ENV['JWT_SECRET'], ['HS256']);
        return $decoded;
        // } catch (\Exception $e) {
        // return null;
        // }
    }

}